package com.test.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

// import org.apache.log4j.Logger;

@SuppressWarnings("unchecked")
public class PaginationUrl {

	/**
	 * The <code>Logger</code> instance for this application.
	 */
	// private static Logger logger = Logger.getLogger(PaginationUrl.class);
	public static void replaceNav(Map pageMap, Pagination pagination, HttpServletRequest request) throws UnsupportedEncodingException {
		String url = request.getRequestURI() + "?";
		Iterator iterator = pageMap.keySet().iterator();
		while (iterator.hasNext()) {
			String param = iterator.next().toString();
			// logger.debug("[param] = " + param);
			// logger.debug("[(String) pageMap.get(param)] = " + (String)
			// pageMap.get(param));
			url += param + "=" + URLEncoder.encode((String) pageMap.get(param), "UTF-8") + "&";
		}
		// url = url.substring(0, url.length()-1);
		// replace url in template
		String previousUrl = url + "page=" + pagination.getPrePage();
		String nextUrl = url + "page=" + pagination.getNextPage();
		String firstUrl = url + "page=1";
		String lastUrl = url + "page=" + pagination.getTotalPages();
		String skipToUrl = url + "page=";
		request.setAttribute("pagination", pagination);

		request.setAttribute("previousUrl", previousUrl);
		request.setAttribute("nextUrl", nextUrl);
		request.setAttribute("firstUrl", firstUrl);
		request.setAttribute("lastUrl", lastUrl);
		request.setAttribute("skipToUrl", skipToUrl);
		// 有疑问？？？
		request.setAttribute("currentUrl", request.getRequestURI());
	}

	public static void replaceNav(Map pageMap, Pagination pagination, HttpServletRequest request, String method) throws UnsupportedEncodingException {
		String url = request.getRequestURI() + "?";
		Iterator iterator = pageMap.keySet().iterator();
		while (iterator.hasNext()) {
			String param = iterator.next().toString();
			// logger.debug("[param] = " + param);
			// logger.debug("[(String) pageMap.get(param)] = " + (String)
			// pageMap.get(param));
			url += param + "=" + URLEncoder.encode((String) pageMap.get(param), "UTF-8") + "&";
		}
		// url = url.substring(0, url.length()-1);
		// replace url in template
		String previousUrl = url + "page=" + pagination.getPrePage() + "&method=" + method;
		String nextUrl = url + "page=" + pagination.getNextPage() + "&method=" + method;
		String firstUrl = url + "page=1" + "&method=" + method;
		String lastUrl = url + "page=" + pagination.getTotalPages() + "&method=" + method;

		String skipToUrl = url + "method=" + method + "&page=";
		request.setAttribute("pagination", pagination);

		request.setAttribute("previousUrl", previousUrl);
		request.setAttribute("nextUrl", nextUrl);
		request.setAttribute("firstUrl", firstUrl);
		request.setAttribute("lastUrl", lastUrl);
		request.setAttribute("skipToUrl", skipToUrl);
		// 有疑问？？？
		request.setAttribute("currentUrl", request.getRequestURI());
	}
}